<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/27
 * Time: 12:28 PM
 */

namespace api\models;

use common\libs\Constants;
use yii\helpers\ArrayHelper;
use api\models\Image;

class Trash extends Article
{
    public $images;

    public function rules()
    {
        $rules = parent::rules();

        return ArrayHelper::merge($rules, [
            [['images'], 'safe']
        ]);
    }

    public function scenarios()
    {
        return [
            'default' => ['user_id', 'id', 'summary', 'created_at', 'images']
        ];
    }

    public function fields()
    {
        return [
            "id",
            'user_id',
            'user_avatar'  => function ($model) {
                /**
                 * @var Trash $model
                 */

                $wx_avatar = $model->user->wx_avatar;
                if (empty($wx_avatar)) {
                    $wx_avatar = '';
                }

                return ! empty($model->user->avatar0) ? $model->user->avatar0->getImageUrl() : $wx_avatar;
            },
            'user_name'    => function ($model) {
                return $model->user->username;
            },
            "summary",
            'images'       => function ($model) {
                $images = ArticleImage::findAll(['aid' => $model->id]);
                $res    = [];
                if ( ! empty($images)) {
                    foreach ($images as $image) {
                        array_push($res, $image->image->imageurl);
                    }
                }

                return $res;
            },
            'scanCount'    => function ($model) {
                return $model->scan_count * 100;
            },
            'commentCount' => function ($model) {
                /**
                 * @var Trash $model
                 */
                return (int)$model->getComments()->count('id');
            },
            'comments'     => function ($model) {
                /**
                 * @var Trash $model
                 */
                $comments = (new Comment())->getCommentByAid($model->id);

                return $comments;
            },
            'created_at'   => function ($model) {
                return date("Y-m-d H:i:s", $model->created_at);
            }

        ];
    }

    public function beforeSave($insert)
    {
        $this->title   = "吐槽";
        $this->user_id = \Yii::$app->user->id;

        return parent::beforeSave($insert);
    }


    public function afterSave($insert, $changedAttributes)
    {
//        var_dump($this->images);
        if ( ! empty($this->images)) {
            foreach ($this->images as $image) {
//                var_dump($image);
                $model           = new ArticleImage();
                $model->aid      = $this->id;
                $model->image_id = $image;
                $model->save();
            }
        }
        parent::afterSave($insert, $changedAttributes);
    }

    public function getComments()
    {
        return $this->hasMany(Comment::className(), ['aid' => 'id'])->where(['status' => Constants::COMMENT_PUBLISH]);
    }

    public function getCommentCount()
    {
        return $this->getComments()->count('id');
    }

}